package gin_http

import (
	"gitee.com/captials-team/ubdframe/src/common/consts"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"github.com/gin-gonic/gin"
	"net/http"
)

// ResponseController 返回Controller
type ResponseController struct {
	ErrIgnoreLog bool //错误返回是否忽略日志
}

func (ctr *ResponseController) Response(ctx *gin.Context, data respdata.ResponseData, msgData ...map[string]string) {
	ctr.ResponseCode(ctx, http.StatusOK, data, msgData...)
}

func (ctr *ResponseController) ResponseCode(ctx *gin.Context, code int, data respdata.ResponseData, msgData ...map[string]string) {
	if ctr.ErrIgnoreLog && data.Code > 0 {
		//支持操作日志log-不记录处理
		ctx.Set(consts.OperateLogContextKey, "")
	}
	ctx.JSON(code, data)
}

func Response(ctx *gin.Context, data respdata.ResponseData, msgData ...map[string]string) {
	ResponseCode(ctx, http.StatusOK, data, msgData...)
}

func ResponseCode(ctx *gin.Context, code int, data respdata.ResponseData, msgData ...map[string]string) {
	ctx.JSON(code, data)
}

func ResponseError(ctx *gin.Context, err error) {
	Response(ctx, respdata.ErrToResponseData(err))
	return
}
